package Servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.KhachHangDAO;
import POJO.KhachHang;

/**
 * Servlet implementation class ModifyKhachHangServlet
 */
//@WebServlet("/ModifyKhachHangServlet")
public class ModifyKhachHangServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyKhachHangServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    String maKhachHang = request.getParameter("maKhachHang");
	    
        KhachHang kh = KhachHangDAO.SelectKhachHangById(maKhachHang);
        
        request.setAttribute("khachHang", kh);        
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("/Modify/ModifyKhachHang.jsp");
        dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    KhachHang khachHang = new KhachHang();
	    if (request.getParameterValues("suaKhachHang") != null)
        {
            String[] strs = request.getParameterValues("suaKhachHang");

            // Chuyển sang chữ có dấu
            strs[0] = new String(strs[0].getBytes("ISO-8859-1"), "UTF-8");
            // Lấy tên
            khachHang.setHoTen(strs[0]);

            //Lấy địa chỉ
            strs[1] = new String(strs[1].getBytes("ISO-8859-1"), "UTF-8");
            khachHang.setDiaChi(strs[1]);

            //Lấy email
            khachHang.setEmail(strs[2]);

            //Lấy dien thoai
            khachHang.setDienThoai(strs[3]);

            //Lấy tinh trang
            strs[4] = new String(strs[4].getBytes("ISO-8859-1"), "UTF-8");
            khachHang.setTinhTrang(strs[4]);

            //Lấy diem tich luy
            khachHang.setDiemTichLuy(Integer.parseInt(strs[5]));

            //Lay ma khach hang
            khachHang.setMaKhachHang(Integer.parseInt(strs[6]));
            
            // Thêm khach hang
            KhachHangDAO.UpdateKhachHang(khachHang);

            request.setAttribute("id", "4");
            ArrayList<KhachHang> list = KhachHangDAO.SelectKhachHangALL();
            
            request.setAttribute("listKhachHang", list);
            RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
            dispatcher.forward(request, response);
        }
	}

}
